package com.ks.BusAlarm;

import java.io.InputStream;

import java.util.List;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.Source;

public class jerichoParser {

	String BusNum;

	jerichoParser() {
	}

	jerichoParser(String pBusNum) {
		BusNum = pBusNum;
	}

	public int parseBusInfo(InputStream data) {

		try {
			Source source = new Source(data);

			source.fullSequentialParse();

			List<Element> list = source.getAllElements();

			for (Element e : list) {

				if (e.getDepth() == 9 && e.getChildElements().size() == 3) {

					String parsedBusNum = e.getChildElements().get(0)
							.getChildElements().get(1).getTextExtractor()
							.toString();

					ksLog.d("parsedBusNum", parsedBusNum);

					if (parsedBusNum.compareTo(BusNum) == 0) {

						String LeftTime = e.getChildElements().get(2)
								.getChildElements().get(1).getTextExtractor()
								.toString().trim();

						if (LeftTime.compareTo("출발대기중") == 0) {
							return -100;
						}
						else if (LeftTime.compareTo("운행종료") == 0) {
							return -200;
						}
						else {
							return Integer.parseInt(LeftTime.replace("분후", ""));
						}
					}
				}

			}

		} catch (Exception e) {

		}

		return -1;
	}
}
